-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(scales): use bisect to handle invertWithStep #200
Conversation
Codecov Report
@@ Coverage Diff @@
## master #200 +/- ##
=========================================
Coverage ? 97.09%
=========================================
Files ? 35
Lines ? 1862
Branches ? 256
=========================================
Hits ? 1808
Misses ? 47
Partials ? 7
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tested locally & code lgtm, just a couple of minor comments about types
@@ -26,7 +28,7 @@ describe('Scale Continuous', () => { | |||
expect(scale.invert(100)).toBe(endTime.toMillis()); | |||
}); | |||
test('check if a scale is log scale', () => { | |||
const domain = [0, 2]; | |||
const domain: any[] = [0, 2]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have a Domain
type that could be used here.
src/state/crosshair_utils.ts
Outdated
@@ -76,7 +77,7 @@ export function getCursorBandPosition( | |||
} | |||
const isHorizontalRotated = isHorizontalRotation(chartRotation); | |||
const snappedPosition = getSnapPosition( | |||
xScale.invertWithStep(isHorizontalRotated ? x : y), | |||
xScale.invertWithStep(isHorizontalRotated ? x : y, data), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since invertWithStep
may return undefined
for continuous scales, do we need to update the type signature for getSnapPosition
? Currently getSnapPosition
defines value: string | number
for its first parameter, but if the scale is a continuous scale, it's possible that the return value of invertWithStep
is undefined
. (I think we already handle if the scaled value is undefined, so the implementation doesn't need to change but rather just the type signature to more accurately reflect the possible values) Further, should we tighten the return type of invertWithStep
in the Scale
interface? Currently it is defined as (value: number, data: any[]) => any
but getSnapPosition
expects string | number
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in its Scale
parent class, invertWithScale
return type is any
because at runtime we can be never sure that the data
object passed to the invertWithScale
function contains only numbers. I've add few more checks on d42bf42 for getCursorBandPosition
and for invertWithStep
to avoid problems with undefined/null values. We should however check all the scales and the data types to have a correct handling of null/undefined values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 we could decode runtime values with something like io-ts
but for now manually validating the data types within these functions works
Codecov Report
@@ Coverage Diff @@
## master #200 +/- ##
=========================================
Coverage ? 96.99%
=========================================
Files ? 35
Lines ? 1865
Branches ? 256
=========================================
Hits ? 1809
Misses ? 49
Partials ? 7
Continue to review full report at Codecov.
|
🎉 This PR is included in version 4.0.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
## [4.0.1](elastic/elastic-charts@v4.0.0...v4.0.1) (2019-05-02) ### Bug Fixes * **scales:** use bisect to handle invertWithStep ([opensearch-project#200](elastic/elastic-charts#200)) ([d988e8b](elastic/elastic-charts@d988e8b)), closes [opensearch-project#195](elastic/elastic-charts#195) [opensearch-project#183](elastic/elastic-charts#183)
Summary
close #195
close #183
We actually relay on the fact that data is evenly distributed among our x axis. We also rely on the
minInterval
value (the minimum interval between two consecutive x values on your dataset) to determine the current interval between x values.Those wrong assumptions leads to issues when interacting with a chart: the interaction invert the pixel coordinate of the mouse to a scale value: the
invertWithStep
function to get the exactx
value of the dataset for a bar chart with linear x axis, using the minInterval and the bandwidth to compute it. If we have odd placed x values or a domain that is larger than the current max x value, the tooltip and the highlighter are misaligned and wrongly displayed as in the closing issues #195 #183Instead of relying on the minInterval, I've refactored the code to use a
bisect
function oninvertWithStep
, to get back the right x value from the chart.I've also pixel adjusted few tests, that use the previous
invertWithStep
function.Checklist
Use
strikethroughsto remove checklist items you don't feel are applicable to this PR.src/index.ts
(and stories only import from../src
except for test data & storybook)